# -*- coding: utf-8 -*-
# Project : flacon
# File : controls.py
# (c) Alexander Sokolov sokoloff.a@gmail.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.


from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import *
from PyQt4.QtGui import *


class MultiValuesSpinBox(QtGui.QSpinBox):
   #****************************************************************
   #
   #****************************************************************
    def __init__(self, parent=None):
        QtGui.QSpinBox.__init__(self, parent)
        self._multi = False
        self._modified = False
        self.connect(self, SIGNAL("valueChanged(int)"), self._valueChanged)


   #****************************************************************
   #
   #****************************************************************
    def setValue(self, value):
        if type(value) is list:
            vals = list(set(value))
            if len(vals) == 1:
                self._multi = False
                QtGui.QSpinBox.setValue(self, vals[0])
            else:
                self._multi = True
                QtGui.QSpinBox.setValue(self, self.minimum()-1)

        else:
            self._multi = False
            QtGui.QSpinBox.setValue(self, value)

        self._modified = False


   #****************************************************************
   #
   #****************************************************************
    def multi(self):
        return self.value() < self.minimum()


   #****************************************************************
   #
   #****************************************************************
    def isModified(self):
        return self._modified


   #****************************************************************
   #
   #****************************************************************
    def textFromValue(self, value):
        if self.value() < self.minimum():
            return QString()
        else:
            return QtGui.QSpinBox.textFromValue(self, value)


   #****************************************************************
   #
   #****************************************************************
    def stepEnabled (self):
        result = QAbstractSpinBox.StepNone

        if self.value() > self.minimum():
            result = result | QAbstractSpinBox.StepDownEnabled

        if self.value() < self.maximum():
            result = result | QAbstractSpinBox.StepUpEnabled

        return result


   #****************************************************************
   #
   #****************************************************************
    def validate(self, text, pos):
        (res, pos) = QIntValidator(self.minimum(),
                                   self.maximum(),
                                   self
                                  ).validate(text, pos)

        return (res, pos)


   #****************************************************************
   #
   #****************************************************************
    def minimum(self):
        return QSpinBox.minimum(self) + 1


   #****************************************************************
   #
   #****************************************************************
    def setMinimum(self, value):
        QSpinBox.setMinimum(self, value - 1)
        if self.value() < value:
            self.setValue(value)


   #****************************************************************
   #
   #****************************************************************
    def _valueChanged(self, value):
        self._modified = True


class YearSpinBox(MultiValuesSpinBox):
   #****************************************************************
   #
   #****************************************************************
    def textFromValue(self, value):
        if ( value == self.minimum() ) :
          return QtCore.QString('')
        else:
          return MultiValuesSpinBox.textFromValue(self, value)


   #****************************************************************
   #
   #****************************************************************
    def validate(self, text, pos):
        if (text == ''):
            self.setValue(self.minimum())
            return (QtGui.QValidator.Acceptable, pos)

        return MultiValuesSpinBox.validate(self, text, pos)



class MultiValuesLineEdit(QtGui.QLineEdit):
   #****************************************************************
   #
   #****************************************************************
    def __init__(self, parent=None):
        QtGui.QLineEdit.__init__(self, parent)
        self._multi = False


   #****************************************************************
   #
   #****************************************************************
    def setText(self, value):
        if type(value) is list:
            vals = list(set(value))
            if len(vals) == 1:
                QLineEdit.setText(self, vals[0])
            else:
                self._multi = True
                QLineEdit.setText(self, QString())

        else:
            QLineEdit.setText(self, value)



class CodePageComboBox(QComboBox):
   #****************************************************************
   # See http://www.iana.org/assignments/character-sets
   #****************************************************************
    def __init__(self, parent = None):
        QComboBox.__init__(self, parent)

        self.addItem(self.tr("Auto detect", "Codepage auto detection"),     QVariant("AUTODETECT"))
        self.insertSeparator()

        self.addItem(self.tr("Unicode (UTF-8)"),     QVariant("UTF-8"))
        self.addItem(self.tr("Unicode (UTF-16LE)"),  QVariant("UTF-16LE"))
        self.addItem(self.tr("Unicode (UTF-16BE)"),  QVariant("UTF-16BE"))

        self.insertSeparator()

        self.addItem(self.tr("Cyrillic (Win-1251)"), QVariant("windows-1251"))
        self.addItem(self.tr("Cyrillic (CP-866)"),   QVariant("IBM866"))

        self.insertSeparator()

        self.addItem(self.tr("Latin-1 (ISO-8859-1)"),   QVariant("ISO-8859-1"))
        self.addItem(self.tr("Latin-2 (ISO-8859-2)"),   QVariant("ISO-8859-2"))
        self.addItem(self.tr("Latin-3 (ISO-8859-3)"),   QVariant("ISO-8859-3"))
        self.addItem(self.tr("Latin-4 (ISO-8859-4)"),   QVariant("ISO-8859-4"))
        self.addItem(self.tr("Latin-5 (ISO-8859-5)"),   QVariant("ISO-8859-5"))
        self.addItem(self.tr("Latin-6 (ISO-8859-6)"),   QVariant("ISO-8859-6"))
        self.addItem(self.tr("Latin-7 (ISO-8859-7)"),   QVariant("ISO-8859-7"))
        self.addItem(self.tr("Latin-8 (ISO-8859-8)"),   QVariant("ISO-8859-8"))
        self.addItem(self.tr("Latin-9 (ISO-8859-9)"),   QVariant("ISO-8859-9"))
        self.addItem(self.tr("Latin-10 (ISO-8859-10)"), QVariant("ISO-8859-10"))

        self.addItem(self.tr("Latin-13 (ISO-8859-13)"), QVariant("ISO-8859-13"))
        self.addItem(self.tr("Latin-14 (ISO-8859-14)"), QVariant("ISO-8859-14"))
        self.addItem(self.tr("Latin-15 (ISO-8859-15)"), QVariant("ISO-8859-15"))
        self.addItem(self.tr("Latin-16 (ISO-8859-16)"), QVariant("ISO-8859-16"))



   #****************************************************************
   #
   #****************************************************************
    def codePage(self):
        if self.currentIndex() > -1:
            return self.itemData(self.currentIndex()).toString()
        else:
            return QString()


   #****************************************************************
   #
   #****************************************************************
    def setCodePage(self, value):
        if type(value) is list:
            vals = list(set(value))
            if len(vals) == 1:
                self._setCodePage(QVariant(vals[0]))
            else:
                self._multi = True
                self.setCurrentIndex(-1)

        else:
            self._setCodePage(QVariant(value))


   #****************************************************************
   #
   #****************************************************************
    def _setCodePage(self, value):
        n = self.findData(value)
        self.setCurrentIndex(n)


   #****************************************************************
   #
   #****************************************************************
    def insertSeparator(self, index=-1):
        if index < 0:
            index = 99999

        # Compatible with PyQt < 4.4
        try:
            QComboBox.insertSeparator(self, index)
        except:
            pass



class ActionPushButton(QtGui.QPushButton):
   #****************************************************************
   #
   #****************************************************************
    def __init__(self, parent = None):
        QtGui.QPushButton.__init__(self, parent)
        self._defAction = None


   #****************************************************************
   #
   #****************************************************************
    def setDefaultAction (self, action):
        self._defAction = action
        self.connect(action, QtCore.SIGNAL('changed()'), self.actionCnanged)
        self.connect(self, QtCore.SIGNAL('clicked()'), self._clicked)
        self.actionCnanged()


   #****************************************************************
   #
   #****************************************************************
    def actionCnanged(self):
        self.setText(self._defAction.text())
        self.setIcon(self._defAction.icon())
        self.setToolTip(self._defAction.toolTip())
        self.setEnabled(self._defAction.isEnabled())


   #****************************************************************
   #
   #****************************************************************
    def _clicked(self):
        if (self._defAction):
            self._defAction.trigger()


class ItemsLabel(QtGui.QLabel):
   #****************************************************************
   #
   #****************************************************************
    def __init__(self, parent=None):
        QtGui.QLabel.__init__(self, parent)
        self._items = None
        self._currentIndex = -1


   #****************************************************************
   #
   #****************************************************************
    def items(self):
        return self._items


   #****************************************************************
   #
   #****************************************************************
    def setItems(self, items):
        self._items = items
        size = QLabel.minimumSize(self)
        width = 0
        for item in self._items:
            width = max(width, self.fontMetrics().width(item))

        size.setWidth(width)
        self.setMinimumSize(size)


   #****************************************************************
   #
   #****************************************************************
    def currentIndex(self):
        return self._currentIndex


   #****************************************************************
   #
   #****************************************************************
    def setCurrentIndex(self, index):
        self._currentIndex = index
        self.setText(self._items[index])
        size = self.minimumSize()

        #self.adjustSize()
